package com.lanou.connotationjoke.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.activity.WebViewActivity;
import com.lanou.connotationjoke.entity.ReviewDetialEty;
import com.lanou.connotationjoke.tool.MyAppllication;
import com.lanou.connotationjoke.tool.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/23.
 */
public class ReviewMovieRecycleViewAdapter extends RecyclerView.Adapter<ReviewMovieRecycleViewAdapter.MyViewHolder>{
    private Context context;
    private List<ReviewDetialEty> datas;

    public ReviewMovieRecycleViewAdapter(Context context, List<ReviewDetialEty> datas) {
        this.context = context;
        this.datas = datas;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.item_recycleview_review_movie, null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
//        int a = (int) (200+Math.random()*100);
//        ViewGroup.LayoutParams layoutParams = holder.picture.getLayoutParams();
//        layoutParams.height=a;
//        holder.picture.setLayoutParams(layoutParams);
        holder.picture.setDefaultImageResId(R.mipmap.ic_launcher);
        holder.picture.setErrorImageResId(R.mipmap.ic_launcher);
        holder.picture.setImageUrl(datas.get(position).getImgsrc()
                , VolleySingleton.getImageLoader());
        holder.titleTv.setText(datas.get(position).getTitle());
        holder.introTv.setText(datas.get(position).getDigest());
        holder.countTv.setText("" + datas.get(position).getPriority());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MyAppllication.hasNetwork()){
                    int pos = holder.getLayoutPosition();
                    WebViewActivity.goToWebViewActivity(context,datas.get(pos).getUrl_3w());
                }else {
                    Toast.makeText(context, "没有网络", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }


    public void refreshData(List<ReviewDetialEty> datas){
        this.datas = datas;
        notifyDataSetChanged();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        NetworkImageView picture;
        TextView titleTv;
        TextView introTv;
        TextView countTv;
        public MyViewHolder(View itemView) {
            super(itemView);
            picture = (NetworkImageView) itemView.findViewById(R.id.iv_review_game_picture);
            titleTv = (TextView) itemView.findViewById(R.id.tv_review_game_text_title);
            introTv = (TextView) itemView.findViewById(R.id.tv_review_game_text_intro);
            countTv = (TextView) itemView.findViewById(R.id.tv_review_game_text_count);
        }
    }

}
